﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeGenerationPipeline;
using System.Xml;
using System.IO;
using System.Diagnostics;
using System.Xml.Schema;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using TestCodeGenerationPipeline.CodeGenerationServiceReference;

namespace TestCodeGenerationPipeline
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new CodeGenerationPipelineDomainServiceSoapClient();

            var results = client.GetProgrammingLanguages();

            results.RootResults.ToList().ForEach(l => Console.WriteLine(l.LanguageName));

            var languages = results.RootResults; // Extract the real collection from the wrapper    
            
            // Make a modification  

            languages[0].LanguageName = "VB";    
            
            // Wrap it in ChangeSetEntry  
            
            var changeEntry = new ChangeSetEntry();
            changeEntry.Entity = languages[0];  
            changeEntry.Operation = DomainOperation.Update;      // Send the changes back to the server as an array of ChangeSetEntries  

            client.SubmitChanges(new ChangeSetEntry[] { changeEntry });
            results = client.GetProgrammingLanguages();

            results.RootResults.ToList().ForEach(l => Console.WriteLine(l.LanguageName));

            client.Close();

            Console.ReadLine();

            //var regex = new Regex(@"xs:schema/xs:complexType\[@Name='\w+?'\](/xs:Element\[@Name='\w+?'\])*");

            //if (regex.IsMatch(@"xs:schema/xs:complexType[@Name='ArchitectureModel']/xs:Element[@Name='ArchitectureModelID']"))
            //{
            //    Debug.WriteLine("Match!");
            //}

            //if (regex.IsMatch(@"xs:schema/xs:complexType[@Name='ArchitectureModel']"))
            //{
            //    Debug.WriteLine("Match!");
            //}

            //var pipeline = new PipelineService();
            //var schemaStream = File.OpenRead(@"..\..\..\CodeGenerationPipeline\Contracts\CodeGenerationPipelineInterface.xsd");
            //var schemaSet = new XmlSchemaSet();
            //var schema = XmlSchema.Read(schemaStream, (o, e) =>
            //{
            //    Console.WriteLine("{0}", e.Message);
            //});

            //schemaSet.Add(schema);

            //schemaSet.Compile();

            //var xmlStream = File.OpenRead(@"..\..\..\CodeGenerationPipeline\Contracts\Example.xml");

            //var document = XDocument.Load(xmlStream);

            //try
            //{
            //    document.Validate(schemaSet, (o, e) =>
            //    {
            //        Console.WriteLine("{0}", e.Message);
            //    });
            //}
            //catch (Exception ex)
            //{
            //    Debugger.Break();
            //}

            //xmlStream.Close();
            //schemaStream.Close();
        }
    }
}
